/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gracefulmm.dealImage;

import java.util.concurrent.Callable;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author wenbo
 */
public class ImageExecutor implements Callable<Boolean> {
    
    private static final Logger log = Logger.getLogger(ImageExecutor.class);

    private String filePath;
    private String imageSize;

    public ImageExecutor(String filePath, String imageSize) {
        this.filePath = filePath;
        this.imageSize = imageSize;
    }

    @Override
    public Boolean call() throws Exception {
        try {
            log.info("准备裁剪图片!路径："+filePath+",总尺寸："+imageSize);
            if (StringUtils.isNotBlank(filePath) && StringUtils.isNotBlank(imageSize)) {
                String[] listSizes = StringUtils.split(imageSize, ",");
                if (listSizes != null || listSizes.length > 0) {
                    for (String str : listSizes) {
                        String[] size = StringUtils.split(str, "x");
                        if (size != null && size.length == 2) {
                            log.info("正在裁剪图片!路径："+filePath+",尺寸：width:"+Integer.valueOf(size[0])+",height:"+Integer.valueOf(size[1]));
                            ImageTools.saveNewImageAsJpg(filePath, Integer.valueOf(size[0]),
                                    Integer.valueOf(size[1]));
                            log.info("图片裁剪完毕!路径："+filePath+",尺寸：width:"+Integer.valueOf(size[0])+",height:"+Integer.valueOf(size[1]));
                        }
                    }
                }
            }
            log.info("图片裁剪完毕!路径："+filePath+",总尺寸："+imageSize);
        } catch (Exception e) {
            log.error("裁剪图片出现异常!", e);
        }
        return true;
    }
}
